<template>
  <div class="formDiv">
    <el-card class="box-card">
      <div class="back">
        <div>
          <el-button
            class="submission priamry_border"
            style="display: flex; float: right"
            @click="back()"
            >返回</el-button
          >
        </div>
      </div>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        clearable
        label-width="102px"
        :rules="rules"
        class="formArea"
      >
        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 酒店名称 -->
              <el-form-item label="酒店名称 :" prop="jdId">
                <el-select v-model="ruleForm.jdId" placeholder="请选择酒店">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.jdName"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 账号名 -->
              <el-form-item label="账号名：" prop="account">
                <el-input v-model="ruleForm.account"></el-input>
              </el-form-item></div
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 账号密码 -->
              <el-form-item label="账号密码：" prop="password">
                <el-input v-model="ruleForm.password"></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="12"
            ><div>
              <!-- 平台 -->
              <el-form-item label="平台：" prop="platform">
                  <el-select
                  v-model="ruleForm.platform"
                  clearable
                  placeholder="请选择平台"
                  class="searchContent"
                >
                  <el-option
                    v-for="item in options1"
                    :key="item.label"
                    :label="item.label"
                    :value="item.label"
                  >
                  </el-option>
                </el-select>
              </el-form-item></div
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><div>
              <!-- 状态 -->
              <el-form-item label="状态" prop="status">
                <el-tooltip
                  effect="dark"
                  :content="ruleForm.status == '0' ? '禁用' : '启用'"
                  placement="right"
                >
                  <el-switch
                    v-model="ruleForm.status"
                    :active-value="1"
                    :inactive-value="0"
                  >
                  </el-switch>
                </el-tooltip>
              </el-form-item></div
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><div>
              <el-form-item class="buttons">
                <el-button type="primary" @click="submitForm('ruleForm')"
                  >保存</el-button
                >
                <el-button @click="cancel()">取消</el-button>
              </el-form-item>
            </div></el-col
          >
          <el-col :span="12"><div></div></el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { groggeryListApi } from "@/api/roomInformation";
import { getPlatformApi, editPlatformApi } from "@/api/flatUser";
export default {
  name: "userAdd",
  created() {
    this.gethotalList(); // 获取所有的酒店
    this.getEditUser(); ////根据 ID 查询平台账号
  },
  data() {
    return {
      //所有平台名称
      options1: [
        {
          value:0,
          label:'美团'
        },
        {
          value:1,
          label:'携程'
        }
      ], 
      options: [], //所有酒店名称
      //表单
      ruleForm: {
        account: "", //账号名
        password: "", //密码
        status: 1, //状态
        id: 0,
      },
      //查询所有酒店
      hotalForm: {
        limit: 10,
        page: 1,
      },
      rules: {
        //酒店名称
        jdId: [
          { required: true, message: "请选择酒店名称", trigger: "change" },
        ],
        //账号名
        account: [
          { required: true, message: "请输入账号名称", trigger: "blur" },
        ],
        //密码
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        //平台
        platform: [
          { required: true, message: "请输入平台", trigger: "change" },
        ],
        //状态
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },
  methods: {
    //返回上一页
    back() {
      this.$router.go(-1);
    },
    // 获取所有的酒店
    async gethotalList() {
      const res = await groggeryListApi(this.hotalForm);
      this.options = res.list;
    },

    //根据 ID 查询平台账号
    async getEditUser() {
      const id = JSON.parse(sessionStorage.getItem("data"));
      const res = await getPlatformApi({ id });
      this.ruleForm = res;
    },
    //点击保存后的提交
    submitForm() {
      //兜底校验
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return;
        //通过后发请求
        await editPlatformApi(this.ruleForm);
        this.$message.success("修改成功"); //提示用户
        this.$refs.ruleForm.resetFields(); //清空表单
        this.$router.push("/amszs/flatUser/index"); //跳转到首页
      });
    },
    //取消按钮
    cancel() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss">
.formDiv {
  margin-top: 40px;
  margin-left: 30px;
  .back {
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: end;
  }
  .formArea {
    padding: 0 30px;
    .picUpload {
      width: 800px;
      height: 100px;
      .upLoadPicBox {
        display: flex;
        width: 800px;
        height: 70px;
      }
    }
    .pictrue {
      width: 60px;
      height: 60px;
      border: 1px dotted rgba(0, 0, 0, 0.1);
      margin-right: 10px;
      position: relative;
      cursor: pointer;
    }
    .btndel {
      position: absolute;
      z-index: 1;
      width: 20px !important;
      height: 20px !important;
      left: 46px;
      top: -4px;
    }
  }
  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }

  .el-input__inner {
    width: 30vw;
  }
  .el-select .el-select--medium {
    width: 40vw;
  }
  .buttons {
    width: 100%;
    height: 50px;
  }
}
.upload {
  width: 80px;
  height: 80px;
  // display: flex;
  text-align: center;
  line-height: 80px;
  background-color: pink;
  border: 1px solid #fff;
}
</style>